﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;

namespace LcfContols.DataBase.MySQL
{
    /// <summary>
    /// 数据库操作实例类
    /// </summary>
    public static class ExecuteSql
    {
        /// <summary>
        /// 获取数据条数
        /// </summary>
        /// <param name="cmd"></param>
        /// <param name="sql"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static ulong GetCount(MySqlCommand cmd, String sql, params object[] args)
        {
            sql = Sql.GetCountString(sql);
            cmd.CommandText = sql;
            Sql.RegxParams(sql, cmd, args);
            object read = cmd.ExecuteScalar();
            if (read != null)
            {
                return Convert.ToUInt64(read);
            }
            return 0;
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sqlString">查询语句</param>
        /// <param name="args">语句参数</param>
        /// <returns>返回一个字典</returns>
        public static Dictionary<String, object> SelectOne(MySqlCommand cmd,String sqlString, params object[] args)
        {
            sqlString = Sql.One(sqlString);
            cmd.CommandText = sqlString;
            Sql.RegxParams(sqlString, cmd, args);
            MySqlDataReader read = cmd.ExecuteReader();
            return Sql.Result(read);
        }

        /// <summary>
        /// 单行单条结果SQL语句执行（sum，count等等统计）
        /// </summary>
        /// <param name="sqlString">查询语句</param>
        /// <param name="args">语句参数</param>
        /// <returns>返回一个字典</returns>
        public static object SingleReuslt(MySqlCommand cmd, String sqlString, params object[] args)
        {
            cmd.CommandText = sqlString;
            Sql.RegxParams(sqlString, cmd, args);
            return cmd.ExecuteScalar();
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sqlString">查询语句</param>
        /// <param name="args">语句参数</param>
        /// <returns>返回一个字典列表</returns>
        public static List<Dictionary<String, object>> Select(MySqlCommand cmd,String sqlString, params object[] args)
        {
            cmd.CommandText = sqlString;
            Sql.RegxParams(sqlString, cmd, args);
            MySqlDataReader read = cmd.ExecuteReader();
            return Sql.ReusltList(read);
        }
        
        /// <summary>
        /// 执行sql语句
        /// </summary>
        /// <param name="sqlString">sql语句</param>
        /// <param name="args">sql语句参数</param>
        /// <returns>返回是否成功</returns>
        public static bool exec(MySqlCommand cmd,String sqlString, params object[] args)
        {
            cmd.CommandText = sqlString;
            Sql.RegxParams(sqlString, cmd, args);
            cmd.ExecuteNonQuery();
            return true;
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="sqlString">sql语句</param>
        /// <param name="args">语句参数</param>
        /// <returns>返回最新的ID</returns>
        public static long InsertBackID(MySqlCommand cmd,String sqlString, params object[] args)
        {
            IUD(cmd, sqlString, args);
            cmd.Parameters.Clear();
            cmd.CommandText = "SELECT last_insert_id()";
            return Convert.ToInt64(cmd.ExecuteScalar());
        }

        /// <summary>
        /// 插入修改删除数据
        /// </summary>
        /// <param name="sqlString">sql语句</param>
        /// <param name="args">语句参数</param>
        /// <returns>修改的数据条数如果失败，返回-1</returns>
        public static int IUD(MySqlCommand cmd,String sqlString, params object[] args)
        {
            cmd.CommandText = sqlString;
            Sql.RegxParams(sqlString, cmd, args);
            return cmd.ExecuteNonQuery();
        }
        
    }
}
